package com.teach.socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Administrator on 2020/7/19.
 */
public class Server {
    public static void service() throws Exception{
        //监听端口
        int port = 8080;
        String info = null;
        ServerSocket serverSocket = new ServerSocket(port);
        //监听来自客户端的连接
        Socket socket = serverSocket.accept();

        InputStreamReader in = new InputStreamReader(socket.getInputStream());
        BufferedReader bf = new BufferedReader(in);
        while ((info = bf.readLine()) != null){
            System.out.print("server=======" + info);
        }
        socket.shutdownInput();
        PrintWriter os = new PrintWriter(socket.getOutputStream());
        os.write("我是服务端");
        os.flush();
        socket.shutdownOutput();

        in.close();
        bf.close();
        os.close();
        socket.close();
        serverSocket.close();
    }
    public static void main(String[] args) throws Exception{
        service();
    }
}
